<?php

namespace app\index\controller;

//加载model
use app\index\model\LoginModel;
use app\index\model\UserModel;

class Index extends CommonController {

    public $login_model;
    public $user_model;

    function __construct() {
        parent::__construct();
        $this->login_model = new LoginModel();
        $this->user_model = new UserModel();
    }

    public function index() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $user_info = $this->login_model->check_user(trim($data['name']), trim($data['pwd']));
            if (empty($user_info) || $user_info['status'] != '0') {
                $this->error('登录失败,请重试');
            }
            //生成加密参数
//            $encode_uid = pbulic_encode($user_info['id']);
//            //加密token
            $uid_token = md5($user_info['id']);
            //设置cookie
            cookie('user_token', $uid_token);
            //保存用户基本信息到Redis
            $user_cace['user_info'] = $user_info;
            $this->redis->redis_set($uid_token, json_encode($user_cace), 7200);
            //跳转到聊天页面
            $this->redirect(url('chat/index'), 302);
        } else {
            return view('index');
        }
    }

}
